package com.xc.hotelmgr.webapi.modules.dt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xc.hotelmgr.webapi.modules.dt.entity.DtEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtService;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;



/**
 * 分销商
 *
 * @author kemmy
 * @email kemmy@gmail.com
 * @date 2020-09-10 16:44:23
 */
@RestController
@RequestMapping("dt/dt")
public class DtController extends AbstractController {
    @Autowired
    private DtService dtService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dt:dt:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("dtId",getUserDtId());
        PageUtils page = dtService.queryPage(params);
        dtService.setParentName( (List<DtEntity>) page.getList());
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dt:dt:info")
    public R info(@PathVariable("id") Integer id){
		DtEntity dt = dtService.getById(id);

        DtEntity pdt = dtService.getById(dt.getParentId());
        if(pdt!= null) {
            dt.setParentName(pdt.getName());
        }
        return R.ok().put("dt", dt);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dt:dt:save")
    public R save(@RequestBody DtEntity dt){
		dtService.save(dt);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dt:dt:update")
    public R update(@RequestBody DtEntity dt){
		dtService.updateById(dt);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dt:dt:delete")
    public R delete(@RequestBody Integer[] ids){
		dtService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
